package com.order.distribute.service.impl;
 
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.order.distribute.base.mapper.BasesMapper;
import com.order.distribute.base.service.impl.BasesServiceImpl;
import com.order.distribute.mapper.DistributeMapper;
import com.order.distribute.pojo.Distribute;
import com.order.distribute.service.DistributeService;
import com.order.distribute.uitl.BaseRespons;
import lombok.extern.slf4j.Slf4j;
 
@Service
@Slf4j
public class DistributeServiceImpl extends BasesServiceImpl<Distribute> implements DistributeService{
    @Autowired
	private DistributeMapper distributeMapper;
	@Override
	public BasesMapper<Distribute> getBasesMapper() {
		 
		return distributeMapper;
	}
	@Override
	public BaseRespons saveDistribute(String orderNo) {
		BaseRespons b =new BaseRespons();
		try {
		
			//幂等处理
	        QueryWrapper<Distribute> wrapper = new QueryWrapper<>();
	        Map hashMap = new HashMap();
	        hashMap.put("order_no", orderNo);
	        wrapper.allEq(hashMap);
	        Distribute distribute = distributeMapper.selectOne(wrapper);
	        if (distribute != null) {
	            
	            log.info("订单已存在，返回已经存在接受");
	            b.setState(0);
	            b.setMsg("订单已存在");
	        }
	        distribute = new Distribute();
           // distribute.setOrderNo(orderNo);
	        distribute.initDistribute();
           // distribute.setDistributeNo("100");
	        Boolean isInsertSuccess = distributeMapper.insert(distribute) == 1L;
	        if (isInsertSuccess) {
	            b.setState(1);
	            b.setMsg("提交成功");
	            log.info("提交成功");
	        } 
		}catch(Exception e) {
		e.printStackTrace();
			    b.setState(-1);
	            b.setMsg("分单系统异常");
	            log.info("分单系统异常");
		}
		return b;
	}
     
}
